<?php


namespace app\common\model\order;


use think\Model;

/**
 * 订单账单
*/
class OrderStatementModel extends Model
{
    protected $name = 'order_statement';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

//    protected $hidden = ['createtime', 'updatetime', 'deletetime'];
    // //列表动态隐藏字段
    // protected static $list_hidden = ['content', 'params', 'images', 'service_ids'];

    /**
     * @param string $statement_type 账单类型:order=销售单,aftersales=售后单
     * @param object|array $order 订单数据(销售单/售后单)
     * 1.支付成功(现结),2.账期订单结算,3.售后退款完成
    */
    public static function operAdd($statement_type = 'order', $order = null)
    {
        $self = new self();
        $self->statement_type = $statement_type;
        switch ($statement_type){
            case 'order':
                $self->user_id = $order['user_id'];
                $self->order_id = $order['id'];
                break;
            case 'aftersales':
                $self->user_id = $order['user_id'];
                $self->order_id = $order['order_id'];
                $self->refund_order_id = $order['id'];
                break;
        }

        $self->save();

        return $self;
    }



    /**
     * 账单分页列表(过滤售后单)
     * @ApiMethod POST
     * @param int $user_id
     * @param string $start_time 开始时间
     * @param string $end_time 结束时间
     * @param int $page
     * @param int $limit
     */
    public function getStatementPageList($params,$user_id)
    {
        extract($params);

        try {
            $field = ['id','statement_type','user_id','order_id','createtime'];
            $where = [
                'user_id' => $user_id,
                'statement_type' => 'order',
                'refund_order_id' => 0,
//                'createtime' => ['between',[$start_time,$end_time]],
            ];
            $order = [
                'createtime' => 'desc',
            ];

            $start_time = empty($start_time)?strtotime(datetime(time(),'Y-m-1')):strtotime($start_time);
            $end_time = empty($end_time)?strtotime('+1 day'):strtotime('+1 day',strtotime($end_time));

            $list = $this
                ->field($field)
                ->with(['user' => function($query){
                    $query->field('id,nickname');
                },'ord' => function($query){
                    $query->field('id,order_no,pay_price');
//                    $query->where('is_sales',0);
                }])
                ->where($where)
                ->whereBetween('createtime',[$start_time,$end_time])
                ->order($order)
                ->paginate($limit, false, ['page' => $page]);
            $order_ids = $this->where($where)->whereBetween('createtime',[$start_time,$end_time])->column('order_id');
            $totle_count = OrderModel::whereIn('id',$order_ids)->count();
            $totle_fee = OrderModel::whereIn('id',$order_ids)->sum('pay_price');

            foreach ($list as $row){
                $row->createtime = datetime($row->createtime);
            }
            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => [
                    'totle_count' => $totle_count,
                    'totle_fee' => $totle_fee,
                    'start_time' => datetime($start_time),
                    'end_time' => datetime($end_time),
                    'list' => $list
                ]
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }
    }

    public function user(){
        return $this->belongsTo('app\common\model\User','user_id','id');
    }

    public function ord(){
        return $this->belongsTo('app\common\model\order\OrderModel','order_id','id');
    }

}